package P2PConnection;

import Commands.InstructionPack;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * Authors: Henrique Alberto Rusa and Danilo Barbosa Porto
 * Date: 03/12/13
 *
 * The SocketClient class implements the object that will connect to the SocketServer.
 * It will send over the connection a instruction referred to the utilization of remote methods on files.
 */
public class SocketClient {

    private final int portNumber = 9765;

    /*
     * Socket where the server will accept the connection to this handler
     */
    private Socket socket;

    /*
     *  Object Stream where the handler will send objects through out the OutputStream associated to this Object
     */
    private ObjectOutputStream _out;

    /*
     *  Object of the type instruction where the server will receive information through the connections made to
     *  the socket.
     */
    private InstructionPack commandRequest;

   /*
    * Object to realize the connection with the client, referencing the host to send the information
    */
    private InetAddress host;

    /*
     *  Constructor of the class.
     *  Instantiates the object output and input, calling the start method to run the connection and send the instruction.
     */
    public SocketClient(InetAddress host) throws IOException {
            this.host = host;
            this.socket = new Socket(host,portNumber);

            //Instances of object output
            _out = new ObjectOutputStream(socket.getOutputStream());
    }

    /*
     * Method that starts the job of the thread and ends the connection when it is done
     */
    public void start(Object instruction){
        try {
            // Receiving the instruction object over the connection
           _out.writeObject(instruction);
           _out.close();
        } catch (IOException e) {
            //TODO
        }
    }
}




























